﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using CommunicatorHistories.Managers;
using CommunicatorHistoryViewer.Models;
using Microsoft.Win32;

namespace CommunicatorHistoryViewer
{
    public partial class MainWindow : Window
    {
        private CommunicatorMessageWrapper historyManager;

        private List<string> Contacts { get; set; }

        public MainWindow()
        {
            ApplicationCheckAndSetupSystemSettins();

            InitializeComponent();

            this.SearchBox.KeyUp += new KeyEventHandler(SearchBox_KeyUp);
            this.AllContacts.MouseLeftButtonUp += new MouseButtonEventHandler(AllContacts_MouseLeftButtonUp);

            this.Hide();

            historyManager = new CommunicatorMessageWrapper();
            historyManager.RunCommunicatorHistoryWather();
            historyManager.OnContactListChanged += new ContactListChangedEventHandler(historyManager_OnContactListChanged);

            this.Contacts = this.historyManager.Contacts;
            this.InitializeContactList(this.Contacts);

            System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon();
            ni.Icon = new System.Drawing.Icon(Application.GetResourceStream(new Uri("/CommunicatorHistoryViewer;component/history.ico", UriKind.Relative)).Stream);
            ni.Visible = true;
            ni.Click +=
                delegate(object sender, EventArgs args)
                {
                    this.WindowState = WindowState.Normal;
                    this.Show();
                    this.Activate();
                };
        }

        private void ApplicationCheckAndSetupSystemSettins()
        {
            string ProgramName = Process.GetCurrentProcess().ProcessName;

            //RegistryKey run = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
            //string ProgramPath = String.Format("\"{0}\"", System.Reflection.Assembly.GetExecutingAssembly().Location);
            //if (run.GetValue(ProgramName) == null && Convert.ToString(run.GetValue(ProgramName)) != ProgramPath)
            //{
            //    run.SetValue(ProgramName,ProgramPath);
            //}

            if (Process.GetProcessesByName(ProgramName).Count() > 1)
            {
                App.Current.Shutdown();
            }
        }

        void AllContacts_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.InitializeContactList(this.Contacts);
        }

        void SearchBox_KeyUp(object sender, KeyEventArgs e)
        {
            this.InitializeContactList(this.Contacts.Where(c => c.ToUpper().Contains(this.SearchBox.Text.ToUpper())).ToList());
        }

        protected override void OnStateChanged(EventArgs e)
        {
            if (WindowState == WindowState.Minimized)
            {
                this.Hide();
            }

            base.OnStateChanged(e);
        }

        private void InitializeContactList(List<string> contacts)
        {
            ContactsList.Children.Clear();
            foreach (var contact in contacts)
            {
                Controls.Contact c = new Controls.Contact(new ContactViewModel
                {
                    Name = contact,
                    HistoryManager = this.historyManager
                });
                ContactsList.Children.Add(c);
            }
        }

        void historyManager_OnContactListChanged(object sender, ContactListEventArgs e)
        {
            this.Contacts = e.Contacts;
            this.Dispatcher.Invoke(new Action(() => this.InitializeContactList(e.Contacts)));
        }

        #region [ Events ]

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void ButtonClose_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Hide();
        }

        private void ButtonClose_MouseEnter(object sender, MouseEventArgs e)
        {
            ButtonClose.Foreground = new SolidColorBrush(Color.FromRgb(12, 149, 214));
        }

        private void ButtonClose_MouseLeave(object sender, MouseEventArgs e)
        {
            ButtonClose.Foreground = new SolidColorBrush(Color.FromRgb(80, 80, 80));
        }

        #endregion

        private void ResizeGrip_MouseDown(object sender, MouseButtonEventArgs e)
        {
        }
    }
}
